package com.funny.learning.english.util;

import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.content.SharedPreferences;

import com.funny.learning.english.model.UserEntity;

public class PreferenceShUtil {
	
	public static final String KEY_ACC = "acc";
	
	public static final String KEY_LIST_LANG = "list_lang";
	public static final String KEY_VALUE_REG_LANG = "lang_reg";
	public static final String KEY_LIST_course = "list_course";
	public static final String KEY_VALUE_REG_COURSE = "course_reg";
	
	public static final String KEY_VALUE_LST_TOP_SUB_GAME = "list_";
	
	
	public static final String KEY_DATA_ACC_ID = "id";
	public static final String KEY_DATA_ACC_IID = "iid";
	public static final String KEY_DATA_ACC_L_NAME = "lname";
	public static final String KEY_DATA_ACC_NAME = "name";
	public static final String KEY_DATA_ACC_MAIL = "mail";
	public static final String KEY_DATA_ACC_AVATAR = "avatar";
	public static final String KEY_DATA_ACC_STATUS = "status";
	public static final String KEY_DATA_ACC_UHASH = "uhash";
	public static final String KEY_DATA_ACC_TOKEN = "token";

	public static void setValue(Context ctx, String key, String val){
		SharedPreferences pref = ctx.getSharedPreferences("TA", 0);
		SharedPreferences.Editor edit = pref.edit();
		edit.putString(key, val);
		edit.commit();
	}
	
	public static void setValue(Context ctx, String key, int val){
		SharedPreferences pref = ctx.getSharedPreferences("TA", 0);
		SharedPreferences.Editor edit = pref.edit();
		edit.putInt(key, val);
		edit.commit();
	}
	
	public static int getValue(Context ctx, String key, int delVal){
		SharedPreferences pref = ctx.getSharedPreferences("TA", 0);
		return pref.getInt(key, delVal);
	}
	public static String getValue(Context ctx, String key, String delVal){
		SharedPreferences pref = ctx.getSharedPreferences("TA", 0);
		return pref.getString(key, delVal);
	}
	public static void removeValue(Context ctx, String key){
		SharedPreferences settings = ctx.getSharedPreferences("TA", 0);
		SharedPreferences.Editor edit = settings.edit();
		edit.remove(key).commit();
	}
	
	public static UserEntity loadJson(String json){
		UserEntity accountEntity = null;
		try {
			JSONObject jsonObject = new JSONObject(json);
			String id = jsonObject.getString(KEY_DATA_ACC_ID);
			int iid = jsonObject.getInt(KEY_DATA_ACC_IID);
			String lName = jsonObject.getString(KEY_DATA_ACC_L_NAME);
			String name = jsonObject.getString(KEY_DATA_ACC_NAME);
			String mail = jsonObject.getString(KEY_DATA_ACC_MAIL);
			String avatar = jsonObject.getString(KEY_DATA_ACC_AVATAR);
			String status = jsonObject.getString(KEY_DATA_ACC_STATUS);
			String uhash = jsonObject.getString(KEY_DATA_ACC_UHASH	);
			String token = jsonObject.getString(KEY_DATA_ACC_TOKEN);
			
			accountEntity = new UserEntity();
			accountEntity.setAvatar(avatar);
			accountEntity.setId(id);
			accountEntity.setIid(iid);
			accountEntity.setMail(mail);
			accountEntity.setLname(lName);
			accountEntity.setName(name);
			accountEntity.setStatus(status);
			accountEntity.setToken(token);
			accountEntity.setUhash(uhash);
			
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return accountEntity;
	}
	
	public static void updateAcc(Context context, UserEntity account){
		String id = account.getId();
		int iid = account.getIid();
		String lName = account.getLname();
		String name = account.getName();
		String mail = account.getMail();
		String avatar = account.getAvatar();
		String status = account.getStatus();
		String uhash = account.getUhash();
		String token = account.getToken();
//		String ts = account.getTimeLogin();
		
		JSONObject jsonObject = new JSONObject();
		try {
			jsonObject.put(KEY_DATA_ACC_ID, id);
			jsonObject.put(KEY_DATA_ACC_ID, iid);
			jsonObject.put(KEY_DATA_ACC_L_NAME, lName);
			jsonObject.put(KEY_DATA_ACC_NAME, name);
			jsonObject.put(KEY_DATA_ACC_MAIL, mail);
			jsonObject.put(KEY_DATA_ACC_AVATAR, avatar);
			jsonObject.put(KEY_DATA_ACC_STATUS, status);
			jsonObject.put(KEY_DATA_ACC_UHASH, uhash);
			jsonObject.put(KEY_DATA_ACC_TOKEN, token);
			
		} catch (JSONException e) {
			e.printStackTrace();
		}
		
		setValue(context, KEY_ACC, jsonObject.toString());
	}
	
	
}
